package Controller;

import Service.IUserService;
import Service.UserServiceImpl;
import bean.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest req, HttpServletResponse resp)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前台页面输入的数据
        String username = req.getParameter("username");
        String pass = req.getParameter("password");
        String message = "";//提示信息
        User user = new User(username, pass);//实例化注册对象，并传入参数
        IUserService userService = new UserServiceImpl();
        HttpSession session = req.getSession();//创建一个session会话，获取当前会话
        //判断注册方法是否返回结果是否为true
        if (userService.registerUser(user)) {
            message = "注册成功！";
        } else {
            message = "注册失败,请重新注册！";
        }
        session.setAttribute("message", message);//设置提示信息内容
        resp.sendRedirect("message.jsp");//重定向至信息提示页面
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest req, HttpServletResponse resp)
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }
}

